const app = getApp()
import user from '../../utils/user'
import {
    getSystemSetUp,
    setPayRepeatTip,
    setSignInMode,
    setNeedAuthSalaryCard,
    setBalanceRemind,
    setFlagUserRemark
} from '../../api/app'
Page({
    data: {
        activityArr: [{
            id: 0,
            label: '拍照打卡'
        },
        {
            id: 1,
            label: '人脸识别'
        }
        ],
        Limitofinsurancearr: [{
            id: 0,
            label: '不限制'
        },
        {
            id: 1,
            label: '仅企业负责人可投'
        }
        ],
        isOpen: false, // 重复发薪提醒
        signInMode: 0, // 拍照打卡
        Limitofinsurance: 0, // 投保限制
        payrollCard: false, // 是否强制开通微工卡,
        balanceRemind: 0, // 是否开启 余额不足提示 0 是不开启 其他数额 是开启
        isBalanceRemind: false,
        moneyArr: ['10000', '50000', '100000', '自定义'],
        isBalanceRemindPopup: false,
        balancePrice: 0, // 弹出输入的价格
        isFlagUserRemark: false,
        isFlagQuicklyGetSalary: false,
        isNeedSignContract: false,
        isCollectEmployeeBankCards: false,
        isCollectOpenBankAddress: false,
        isCollectOpenBankAddressflag: false,
        yhkhhcs: false,
        yhkhhmc: false,
        bankcard: false,
        isSuperAdmin: false,
        showClock: false,
        tempClockValue: ''
    },
    bindPickerChange(e) {
        console.log('e', e)
        const val = Number(e.detail.value)
        setSignInMode({
            signInMode: val
        }).then(() => {
            this.setData({
                signInMode: val
            })
            user.signInMode = val
            app.toast('修改成功', 'success')
        }, () => {
            this.setData({
                signInMode: parseInt(e.detail.value)
            })
        })
    },
    bindPickerChangeLimitofinsurance(e) {
        console.log('e', e)
        const val = Number(e.detail.value)
        setSignInMode({
            Limitofinsurance: val
        }).then(() => {
            this.setData({
                Limitofinsurance: val
            })
            user.Limitofinsurance = val
            app.toast('修改成功', 'success')
        }, () => {
            this.setData({
                Limitofinsurance: parseInt(e.detail.value)
            })
        })
    },
    Transferbyfounder() {
        wx.navigateTo({
            url: '/pages/Transferbyfounder/Transferbyfounder',
        })
    },
    onLoad() {
        user.waitLogin().then((userData) => {
            console.log(2121, userData)
            // 获取当前企业的设置
            getSystemSetUp().then(res => {
                console.log('res', res)
                this.setData({
                    isOpen: res.isOpenPayRepeatReminder,
                    payrollCard: res.isNeedAuthSalaryCard,
                    signInMode: res.signInMode,
                    balanceRemind: user.balanceRemind,
                    isBalanceRemind: !!user.balanceRemind,
                    isFlagUserRemark: !!res.flagUserRemark,
                    isFlagQuicklyGetSalary: !!res.flagQuicklyGetSalary,
                    isNeedSignContract: !!res.isNeedSignContract,
                    isCollectEmployeeBankCards: !!res.isCollectEmployeeBankCards,
                    isCollectOpenBankAddress: !!res.isCollectOpenBankAddress,
                    isCollectOpenBankAddressflag: res.isCollectEmployeeBankCards === 1 ? true : false,
                    yhkhhmc: res.isCollectOpenBankName === 1 ? true : false,
                    yhkhhcs: res.isCollectOpenBankCity === 1 ? true : false,
                    bankcard: res.isCollectBankCardImg === 1 ? true : false,
                    isSuperAdmin: userData.isSuperAdmin
                })
            })
        })

        // getPayRepeatStatus().then(res=>{
        //     this.setData({
        //         isOpen: res
        //     })
        // })
        // this.setData({
        //     signInMode: user.signInMode
        // })
    },
    // 离开当前页面时 更新全局balanceRemind
    onUnload() {
        user.balanceRemind = this.data.balanceRemind
    },
    // 切换是否开启重复发薪提醒
    checkOpen(event) {
        setPayRepeatTip({
            isOpen: event.detail.value
        }).then(() => {
            this.setData({
                isOpen: event.detail.value
            })
            app.toast('修改成功', 'success')
        }, () => {
            this.setData({
                isOpen: this.data.isOpen
            })
        })
    },
    // 切换是否开启强制开通薪工卡
    onPayrollCard(event) {
        const {
            value
        } = event.detail
        console.log(value, 'event')
        setNeedAuthSalaryCard({
            needAuthSalaryCard: value
        }).then(res => {
            this.setData({
                payrollCard: value
            })
            app.toast('修改成功', 'success')
        }, () => {
            this.setData({
                payrollCard: this.data.payrollCard
            })
        })
    },
    // 切换是否开启拍照打卡
    onChangeClockMode(event) {
        const val = Number(event.detail.value)
        setSignInMode({
            signInMode: val
        }).then(() => {
            this.setData({
                signInMode: val
            })
            user.signInMode = val
            app.toast('修改成功', 'success')
        }, () => {
            this.setData({
                signInMode: this.data.signInMode
            })
        })
    },
    // 开启余额不足提示
    onbalanceRemind(event) {
        console.log(event)
        const {
            value
        } = event.detail
        if (value) {
            wx.showActionSheet({
                itemList: this.data.moneyArr,
                success: (res) => {
                    console.log(res.tapIndex)
                    // 点击 自定义之外的金额 进入
                    if (res.tapIndex !== 3) {
                        setBalanceRemind({
                            balanceRemind: this.data.moneyArr[res.tapIndex]
                        }).then(() => {
                            this.setData({
                                isBalanceRemind: true,
                                balanceRemind: this.data.moneyArr[res.tapIndex]
                            })
                            app.toast('修改成功', 'success')
                        })
                    } else {
                        // 点击自定义 进入
                        this.setData({
                            isBalanceRemindPopup: true
                        })
                    }
                },
                fail: (res) => {
                    console.log(res.errMsg)
                    if (!this.data.balanceRemind) {
                        this.setData({
                            balanceRemind: 0,
                            isBalanceRemind: false
                        })
                    }
                }
            })
        } else {
            setBalanceRemind({
                balanceRemind: 0
            }).then(res => {
                this.setData({
                    balanceRemind: 0,
                    isBalanceRemind: false
                })
                app.toast('关闭成功', 'success')
            })
        }
    },
    // 余额不足提示弹窗价格设置
    setBalancePrice() {
        console.log(this.data.balancePrice, 'balancePrice')
        if (this.data.balancePrice < 0) {
            app.toast('不可小于0')
            return
        }
        if (this.data.balancePrice == 0) {
            this.setData({
                balanceRemind: 0,
            })
            this.cancel()
            app.toast('检测到输入为‘0’自动关闭')
        }
        setBalanceRemind({
            balanceRemind: this.data.balancePrice
        }).then(() => {
            this.setData({
                balanceRemind: Number(this.data.balancePrice),
            })
            this.cancel()
            app.toast('修改成功', 'success')
        })
    },
    // 关闭余额不足提示弹窗
    cancel() {
        this.setData({
            isBalanceRemindPopup: false,
        })
        // 检测他是否有设置 用过没有设置则 关闭
        if (!this.data.balanceRemind) {
            this.setData({
                isBalanceRemind: false
            })
        }
    },
    // 前往余额不足提示设置页面
    onBalanceDeficiencyHint() {
        wx.navigateTo({
            url: '/pages/set-balance-deficiency-hint/set-balance-deficiency-hint',
        })
    },
    // 快捷修改余额不足提示 金额
    onQuickSetBalancePrice() {
        this.setData({
            isBalanceRemindPopup: true,
            balancePrice: this.data.balanceRemind // 做金额回显
        })
    },
    // 备注对员工可见
    async onFlagUserRemark(event) {
        console.log(event)
        setFlagUserRemark({
            flagUserRemark: Number(event.detail.value)
        }).then(() => {
            this.setData({
                isFlagUserRemark: event.detail.value
            })
            app.toast('修改成功', 'success')
        }, () => {
            this.setData({
                isFlagUserRemark: this.data.isFlagUserRemark
            })
        })
    },
    onFlagQuicklyGetSalary(event) {
        setFlagUserRemark({
            flagQuicklyGetSalary: Number(event.detail.value)
        }).then(() => {
            this.setData({
                isFlagQuicklyGetSalary: event.detail.value
            })
            app.toast('修改成功', 'success')
        }, () => {
            this.setData({
                isFlagQuicklyGetSalary: this.data.isFlagUserRemark
            })
        })
    },
    onisNeedSignContract(event) {
        setFlagUserRemark({
            isNeedSignContract: Number(event.detail.value)
        }).then(() => {
            this.setData({
                isNeedSignContract: event.detail.value
            })
            app.toast('修改成功', 'success')
        }, () => {
            this.setData({
                isNeedSignContract: this.data.isFlagUserRemark
            })
        })
    },
    // 修改领薪时手机员工银行卡开户行。
    onCollectEmployeeBankCardsopeningbank(event) {
        console.log(event, 'onCollectEmployeeBankCardsopeningbank')
        setFlagUserRemark({
            isCollectOpenBankAddress: Number(event.detail.value)
        }).then(() => {
            this.setData({
                isCollectOpenBankAddress: event.detail.value
            })
            app.toast('修改成功', 'success')
        }, () => {
            this.setData({
                isCollectOpenBankAddress: this.data.isFlagUserRemark
            })
        })
    },
    onCollectEmployeeBankCards(event) {
        console.log(event, 'onCollectEmployeeBankCards')
        if (event.detail.value) {
            this.setData({
                isCollectEmployeeBankCards: true,
            })
            setFlagUserRemark({
                isCollectOpenBankName: 1,
                isCollectOpenBankCity: 1,
                isCollectEmployeeBankCards: 1,
                isCollectBankCardImg: 1,
            }).then(() => {
                app.toast('修改成功', 'success')
                this.setData({
                    yhkhhmc: true,
                    yhkhhcs: true,
                    bankcard: true
                })
            })
        } else if (!event.detail.value) {
            this.setData({
                isCollectEmployeeBankCards: false,
            })
            setFlagUserRemark({
                isCollectOpenBankName: 0,
                isCollectOpenBankCity: 0,
                isCollectEmployeeBankCards: 0,
                isCollectBankCardImg: 0,
            }).then(() => {
                app.toast('修改成功', 'success')
                this.setData({
                    yhkhhmc: false,
                    yhkhhcs: false,
                    bankcard: false
                })
            })
        }
    },
    changeisCollectEmployeeBankCards() {
        if (this.data.isCollectEmployeeBankCards) {
            setFlagUserRemark({
                isCollectOpenBankName: 0,
                isCollectOpenBankCity: 0,
                isCollectEmployeeBankCards: 0
            }).then(() => {
                app.toast('修改成功', 'success')
                this.setData({
                    isCollectEmployeeBankCards: false,
                    yhkhhmc: false,
                    yhkhhcs: false,
                    bankcard: false
                })
            })
        } else {
            setFlagUserRemark({
                isCollectEmployeeBankCards: 1
            }).then(() => {
                app.toast('修改成功', 'success')
                this.setData({
                    isCollectEmployeeBankCards: true,
                })
            })
        }
    },
    changebankcard() {
        if (this.data.bankcard) {
            setFlagUserRemark({
                isCollectBankCardImg: 0,
            }).then(() => {
                app.toast('修改成功', 'success')
                this.setData({
                    bankcard: false
                })
            })
        } else {
            setFlagUserRemark({
                isCollectBankCardImg: 1,
            }).then(() => {
                app.toast('修改成功', 'success')
                this.setData({
                    bankcard: true
                })
            })
        }
    },
    changeyhkhhcs() {
        if (this.data.yhkhhcs) {
            setFlagUserRemark({
                isCollectOpenBankCity: 0,
            }).then(() => {
                app.toast('修改成功', 'success')
                this.setData({
                    yhkhhcs: false
                })
            })
        } else {
            setFlagUserRemark({
                isCollectOpenBankCity: 1,
            }).then(() => {
                app.toast('修改成功', 'success')
                this.setData({
                    yhkhhcs: true
                })
            })
        }
    },
    changeyhkhhmc() {
        if (this.data.yhkhhmc) {
            setFlagUserRemark({
                isCollectOpenBankName: 0,
            }).then(() => {
                app.toast('修改成功', 'success')
                this.setData({
                    yhkhhmc: false
                })
            })
        } else {
            setFlagUserRemark({
                isCollectOpenBankName: 1,
            }).then(() => {
                app.toast('修改成功', 'success')
                this.setData({
                    yhkhhmc: true
                })
            })
        }
    },
    onShowClock() {
        this.setData({
            showClock: true
        })
    },
    onCloseClock() {
        this.setData({
            showClock: false
        })
    },
    onClockChange(e) {
        this.data.tempClockValue = e.detail.value[0]
    },
    onClockConfirm() {
        this.setData({
            signInMode: this.data.tempClockValue,
            showClock: false
        })
        this.bindPickerChange({
            detail: {
                value: this.data.signInMode
            }
        })
        this.data.tempClockValue = 0
    }
})